<?php

namespace app\model;


use think\Model;
use think\facade\Env;
class Task extends Model
{
    protected $autoWriteTimestamp = true;

    /**
     * 创建任务
     *
     * @param [type] $task_data
     * @return void
     */
    public static function createTask($task_data)
    {
        if ($task_data['pay_max'] <= $task_data['pay_min']) {
            error('最高薪资应高于最低薪资');
        }

        if ($task_data['age_max'] <= $task_data['age_min']) {
            error('最大年龄应高于最小年龄');
        }
        $task_data['tag'] = empty($task_data['welfare']) ? json_encode($task_data['tag']) : json_encode([]);
        $task_data['welfare'] = empty($task_data['welfare']) ? json_encode($task_data['welfare']) : json_encode([]);
        $task_data['work_env'] = empty($task_data['welfare']) ? json_encode($task_data['work_env']) : json_encode([]);
        $task_data['is_show'] = 0;
        try {
            self::create((array)$task_data);
        } catch (\Exception $e) {
            error($e->getMessage());
        }

        return true;
    }

    /**
     * 修改任务
     *
     * @param [type] $id
     * @param [type] $task_data
     * @return void
     */
    public static function updateTask($id, $task_data)
    {
        if (!self::where('id', $id)->find()) {
            error('没有此任务');
        }

        if ($task_data['pay_max'] <= $task_data['pay_min']) {
            error('最高薪资应高于最低薪资');
        }

        if ($task_data['age_max'] <= $task_data['age_min']) {
            error('最大年龄应高于最小年龄');
        }

        $task_data['tag'] = empty($task_data['welfare']) ? json_encode($task_data['tag']) : json_encode([]);
        $task_data['welfare'] = empty($task_data['welfare']) ? json_encode($task_data['welfare']) : json_encode([]);
        $task_data['work_env'] = empty($task_data['welfare']) ? json_encode($task_data['work_env']) : json_encode([]);

        try {
            self::where('id', $id)->update($task_data);
        } catch (\Exception $e) {
            error($e->getMessage());
        }

        return true;
    }

    /**
     * 任务上架、下架
     *
     * @param [type] $id
     * @return boolean
     */
    public static function isShow($id)
    {
        $is_show = self::where('id', $id)->value('is_show');
        if ($is_show == 1) {
            $is_show = 0;
            $msg = '上线成功';
        } else {
            $is_show = 1;
            $msg = '下线成功';
        }

        try {
            self::where('id', $id)->update(['is_show' => $is_show]);
        } catch (\Exception $e) {
            error($e->getMessage());
        }

        return $msg;
    }

    public function getTagAttr($value, $data)
    {
        if (empty($value)) {
            return [];
        }
        return json_decode($value, true);
    }

    public function getWelfareAttr($value, $data)
    {
        if (empty($value)) {
            return [];
        }
        return json_decode($value, true);
    }

    public function getWorkEnvAttr($value, $data)
    {
        $value =  json_decode($value, true);
        if (is_array($value)) {
            foreach ($value as $k => $v) {
                if (strpos($v, 'http') !== false) {
                    $value[$k] = $v;
                } else {
                    $value[$k] = Env::get('FILE.PATH') . $v;
                }
            }
        } else {
            $value = [];
        }
        return $value;
    }

    public function getWordAttr($value, $data)
    {
        if (empty($value)) {
            return '';
        }
        if (strpos($value, 'http') !== false) {
            $value = $value;
        } else {
            $value = Env::get('FILE.PATH') . $value;
        }

        return $value;
    }
}